<!DOCTYPE html "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"
    >
<meta charset="UTF-8">
<head th:include="include :: header"></head>

<body class="gray-bg">
<div class="wrapper wrapper-content ">
    <div class="col-sm-12">
        <div class="ibox">
            <div class="ibox-body">
                <div class="fixed-table-toolbar">
                    <div class="columns pull-left">
                        <button type="button"
                                class="btn  btn-primary" onclick="add()">
                            <i class="fa fa-plus" aria-hidden="true"></i>添加
                        </button>
                    </div>
                    <div class="columns pull-right">
                        <button class="btn btn-success" onclick="query()">查询</button>
                        <button class="btn btn-success" onclick="resetParam()">重置</button>
                    </div>

                    <div class="columns pull-right col-md-2 nopadding">
                        <input class="form-control" type="text" id="bannerName" placeholder="轮播图名称" />
                    </div>
                    <div class="columns pull-right col-md-2 nopadding">
                        <select id="status" data-placeholder="状态" class="form-control chosen-select" tabindex="1"
                                style="width: 100%">
                            <option value="">--状态--</option>
                            <option value="1">上架</option>
                            <option value="0">下架</option>
                        </select>
                    </div>

                    <div class="columns pull-right col-md-2 nopadding">
                        <select id="bannerType" data-placeholder="轮播图类型" class="form-control chosen-select" tabindex="1"
                                style="width: 100%">
                            <option value="">--轮播图类型--</option>
                            <option value="1">贷款首页</option>
                            <option value="2">马甲首页</option>
                        </select>
                    </div>

                    <div class="columns pull-right col-md-2 nopadding">
                        <select id="appId" data-placeholder="所属应用" class="form-control chosen-select" tabindex="1"
                                style="width: 100%">
                            <option value="">--所属应用--</option>
                            <option th:each="appInfo,appStat:${appInfos}" th:value="${appInfo.appId}" th:text="${appInfo.appName}"></option>
                        </select>
                    </div>
                    <div class="columns pull-right col-md-2 nopadding">
                        <input class="form-control" type="text" id="createTime" name="createTime" placeholder="创建时间" />
                    </div>
                </div>
            </div>
            <table id="exampleTable" data-mobile-responsive="true">
            </table>
        </div>
    </div>
</div>
</div>
<!--shiro控制bootstraptable行内按钮看见性 来自bootdo的创新方案 -->
<div>
    <script type="text/javascript">
        var s_edit_h = '';
    </script>
</div>
<div >
    <script type="text/javascript">
        s_edit_h = '';
    </script>
</div>
<div>
    <script type="text/javascript">
        var s_edit_d = '';
    </script>
</div>
<div >
    <script type="text/javascript">
        s_edit_d = '';
    </script>
</div>

</div>
<div th:include="include :: footer"></div>
<script type="text/javascript">

    var prefix = "/app/banner";
    $(function () {
        $('#createTime').val('');
        load();
    });

    $('input[name="createTime"]').daterangepicker({
            locale: {
                format: 'YYYY-MM-DD',
                applyLabel: "确认",
                cancelLabel: "取消",
            }
        },
        function(start, end, label) {

            // alert("A new date range was chosen: " + start.format        ('YYYY-MM-DD') + ' to ' + end.format('YYYY-MM-DD'));
        });

    function resetParam() {
        $('#bannerName').val('');
        $('#status').val('');
        $('#bannerType').val('');
        $('#appId').val('');
        $('#createTime').val('');
        reLoad();
    }

    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get', // 服务器数据的请求方式 get or post
                    url: prefix + "/list", // 服务器数据的加载地址
                    //showRefresh: true,
                    //	showToggle : true,
                    //	showColumns : true,
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: true, // 设置为true会有隔行变色效果
                    dataType: "json", // 服务器返回的数据类型
                    pagination: true, // 设置为true会在底部显示分页条
                    // queryParamsType : "limit",
                    // //设置为limit则会发送符合RESTFull格式的参数
                    singleSelect: false, // 设置为true将禁止多选
                    // contentType : "application/x-www-form-urlencoded",
                    // //发送到服务器的数据编码类型
                    pageSize: 10, // 如果设置了分页，每页数据条数
                    pageNumber: 1, // 如果设置了分布，首页页码
                    //search : true, // 是否显示搜索框
                    showColumns: false, // 是否显示内容下拉框（选择显示的列）
                    sidePagination: "server", // 设置在哪里进行分页，可选值为"client" 或者 "server"
                    queryParams: function (params) {
                        return {
                            //说明：传入后台的参数包括offset开始索引，limit步长，sort排序列，order：desc或者,以及所有列的键值对
                            limit: params.limit,
                            offset: params.offset,
                            bannerName: $.trim($('#bannerName').val()),
                            status: $('#status').val(),
                            bannerType: $('#bannerType').val(),
                            appId: $('#appId').val(),
                            createTime:$('#createTime').val()
                        };
                    },
                    // //请求服务器数据时，你可以通过重写参数的方式添加一些额外的参数，例如 toolbar 中的参数 如果
                    // queryParamsType = 'limit' ,返回参数必须包含
                    // limit, offset, search, sort, order 否则, 需要包含:
                    // pageSize, pageNumber, searchText, sortName,
                    // sortOrder.
                    // 返回false将会终止请求
                    columns: [
                        {
                            field: 'bannerId',
                            title: '轮播图编号'
                        },
                        {
                            field: 'bannerName',
                            title: '轮播图名称'
                        },
                        {
                            field: 'linkUrl',
                            title: '链接地址'
                        },
                        {
                            field: 'appName',
                            title: '所属应用'
                        },
                        {
                            field: 'productName',
                            title: '所属产品'
                        },
                        {
                            field: 'createTime',
                            title: '创建时间'
                        },
                        {
                            field: 'status',
                            title: '状态',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '<span class="label label-danger">下架</span>';
                                } else if (value == '1') {
                                    return '<span class="label label-primary">上架</span>';
                                }
                            }
                        },
                        {
                            field: 'bannerType',
                            title: '轮播图类型',
                            formatter: function (value, row, index) {
                                if (value == 1) {
                                    return '<span class="label label-danger">贷款首页</span>';
                                } else if (value == 2) {
                                    return '<span class="label label-primary">马甲首页</span>';
                                }
                            }
                        },
                        {
                            title: '操作',
                            field: 'bannerId',
                            align: 'center',
                            formatter: function (value, row, index) {
                                var dtl = '<a class="btn btn-success btn-sm '  + '" href="#" mce_href="#" title="详情" onclick="dtl(\''
                                    + value
                                    + '\')"><i class="fa fa-address-card-o"></i>详情</a> ';
                                var e = '<a class="btn btn-primary btn-sm ' + s_edit_h + '" href="#" mce_href="#" title="编辑" onclick="edit(\''
                                    + value
                                    + '\')"><i class="fa fa-address-card-o"></i>编辑</a> ';
                                var d='<a class="btn btn-warning btn-sm ' + s_edit_d + '" href="#" mce_href="#" title="删除" onclick="batchRemove(\''
                                    + value
                                    + '\')"><i class="fa fa-address-card-o"></i>删除</a> ';
                                if(row.status == '1'){
                                    var sta = '<a class="btn btn-success btn-sm '  + '" href="#" mce_href="#" title="详情" onclick="status(\''
                                        + value + '\',\'0'
                                        + '\')"><i class="fa fa-address-card-o"></i>下架</a> ';
                                }else if(row.status == '0'){
                                    var sta = '<a class="btn btn-success btn-sm '  + '" href="#" mce_href="#" title="详情" onclick="status(\''
                                        + value + '\',\'1'
                                        + '\')"><i class="fa fa-address-card-o"></i>上架</a> ';
                                }
                                return dtl + e+d+sta;
                            }
                        }]
                });
    }

    function query() {
        var opt = {
            query: {
                bannerName: $.trim($('#bannerName').val()),
                status: $('#status').val(),
                bannerType: $('#bannerType').val(),
                appId: $('#appId').val(),
                createTime:$('#createTime').val()
            }
        };
        $('#exampleTable').bootstrapTable('refresh', opt);
    }

    function reLoad() {
        var opt = {
            query: {
                limit: 10,
                offset: 0
            }
        };
        $('#exampleTable').bootstrapTable('refresh', opt);
    }

    function add() {
        layer.open({
            type: 2,
            title: '增加banner',
            maxmin: true,
            shadeClose: false, // 点击遮罩关闭层
            area: ['1500px', '800px'],
            content: prefix + '/add'// iframe的url
        });
    }

    function edit(id) {
        layer.open({
            type: 2,
            title: '编辑banner',
            maxmin: true,
            shadeClose: false, // 点击遮罩关闭层
            area: ['1500px', '800px'],
            content: prefix + '/edit/' + id// iframe的url
        });
    }

    function dtl(id) {
        layer.open({
            type: 2,
            title: 'banner详情',
            maxmin: true,
            shadeClose: false, // 点击遮罩关闭层
            area: ['950px', '520px'],
            content: prefix + '/dtl/' + id// iframe的url
        });
    }


    function status(id,status) {
        var rows = $('#exampleTable').bootstrapTable('getSelections'); // 返回所有选择的行，当没有选择的记录时，返回一个空数组

        layer.confirm("确认要修改状态吗?", {
            btn: ['确定', '取消']
            // 按钮
        }, function () {
            $.ajax({
                type: 'POST',
                data: {
                    "bannerId": id,
                    "status": status,
                },
                url: prefix + '/status',
                success: function (r) {
                    if (r.code == 0) {
                        layer.msg(r.msg);
                        $('#exampleTable').bootstrapTable('refresh');
                    } else {
                        layer.msg(r.msg);
                    }
                }
            });
        }, function () {
        });

    }


    function batchRemove(id) {
        var rows = $('#exampleTable').bootstrapTable('getSelections'); // 返回所有选择的行，当没有选择的记录时，返回一个空数组

        layer.confirm("确认要删除数据吗?", {
            btn: ['确定', '取消']
            // 按钮
        }, function () {
            $.ajax({
                type: 'POST',
                data: {
                    "bannerId": id
                },
                url: prefix + '/remove',
                success: function (r) {
                    if (r.code == 0) {
                        layer.msg(r.msg);
                        $('#exampleTable').bootstrapTable('refresh');
                    } else {
                        layer.msg(r.msg);
                    }
                }
            });
        }, function () {
        });

    }
</script>
</body>

</body>
</html>